home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / CyberProcDoggie / Source Code / Woof / Woof_Real / ReallWoofItem.cpp next >
Encoding:
C/C++ Source or Header  |  1995-06-24  |  5.8 KB  |  278 lines  |  [TEXT/MPS ]

  1. #include "RealWoofItem.h"
  2.  
  3. #include <string.h>
  4. #include <Icons.h>
  5.  
  6. #include "Draft.xh"
  7. #include "MemMgr.h"
  8. #include "Part.xh"
  9. #include "StorageU.xh"
  10.  
  11. #include "CyberdogDef.h"
  12. #include "Debug.h"
  13. #include "WoofStream.xh"
  14. #include "ODFoci.h"
  15. #include "WoofURL.h"
  16. #include "WoofLibraryInit.h"
  17.  
  18. #define INHERITED    TRealCyberItem
  19.  
  20.  
  21. TRealWoofItem::TRealWoofItem()
  22.     {
  23.     fURL = nil;
  24.     }
  25.     
  26.     
  27.  
  28. TRealWoofItem::~TRealWoofItem()
  29.     {
  30.     if (fURL)
  31.         {
  32.         MMFree(fURL);
  33.         fURL = nil;
  34.         }
  35.     }
  36.     
  37.     
  38.  
  39. void TRealWoofItem::SetUpFromURL(Environment *ev, char* url, StringPtr defaultName)
  40. {
  41.     unsigned short    size;
  42.     unsigned short    hostNameLen;
  43.     unsigned short    procNameLen;
  44.     char*            hostName;
  45.     char*            procName;
  46.     Str255            newName;
  47.     
  48.     ASSERT(fDefaultName == nil);
  49.     ASSERT(fURL == nil);
  50.  
  51.     if (defaultName)
  52.     {
  53.         this->SetDefaultName(ev, defaultName);
  54.     }
  55.     else
  56.     {
  57.         // The host name is located just past the "doggie://"
  58.         // We want the name of the item to be the process name
  59.         // there is no host, or "proc On host" if there is a host.
  60.         
  61.         ParseWoofURL(url, &hostName, &hostNameLen, &procName, &procNameLen);
  62.         
  63.         newName[0] = 0;
  64.         ASSERT(procNameLen > 0);
  65.         
  66.         // We need to make sure we don't overwrite a Str255!!
  67.         if (procNameLen > 0)
  68.         {
  69.             BlockMove(procName, newName + 1, procNameLen);
  70.             newName[0] = procNameLen;
  71.         }
  72.         
  73.         if (hostNameLen > 0)
  74.         {
  75.             BlockMove(" On ", newName + procNameLen, 4);
  76.             BlockMove(hostName, newName + procNameLen + 4, hostNameLen);
  77.             newName[0] += (4 + hostNameLen);
  78.         }
  79.  
  80.         this->SetDefaultName(ev, (StringPtr) newName);
  81.     }
  82.     
  83.     size = strlen(url) + 1;
  84.     fURL = (char *)MMAllocate(size);                                 //•Err
  85.     ASSERT(fURL != nil);
  86.     BlockMove(url, fURL, size);
  87. }
  88.  
  89.     
  90.  
  91. void TRealWoofItem::ExternalizeContent(Environment *ev, ODStorageUnit* su)
  92.     {
  93.     const ODValueType    kODAppleTEXT    = "Apple:OSType:Scrap:TEXT";            // •!• remove this when bug #1151636 has been resolved and kODAppleText (or whatever) has been redefined and works
  94.     
  95.         
  96.     //
  97.     // Add first value, which is of the kind of the CyberViewer, and has the
  98.     // CyberItem as it's value.
  99.     su->AddValue(ev, kTextViewerKind);
  100.     this->StreamToStorageUnit(ev, su);    
  101.  
  102.     //
  103.     // Add second value, which is of kind "CyberItem", and has the
  104.     // CyberItem as it's value. (this is identical to the above value)
  105.     su->AddValue(ev, kCyberItemKind);
  106.     this->StreamToStorageUnit(ev, su);    
  107.     
  108.     //
  109.     // Add third value, which is of the kind of scrapbook text, and has the
  110.     // URL as it's value.
  111.     su->AddValue(ev, kODAppleTEXT);            
  112.     StorageUnitSetValue(su, ev, strlen(fURL), (ODPtr) fURL);
  113. //    su->SetValue(ev, strlen(fURL), (ODValue)fURL);
  114.     }
  115.     
  116.  
  117.  
  118. void TRealWoofItem::StreamToStorageUnit(Environment *ev, ODStorageUnit* su)
  119.     {
  120.     long    length;
  121.  
  122.     ASSERT(fDefaultName);
  123.     ASSERT(fURL);
  124.     
  125.     // CI ClassID Length
  126.     length = strlen(kWoofItemClassName) + 1;
  127.     StorageUnitSetValue(su, ev, sizeof(long), (ODPtr)& length);
  128. //    su->SetValue(ev, sizeof(long), (ODValue)&length);
  129.  
  130.     // CI ClassID
  131.     StorageUnitSetValue(su, ev, length, (ODPtr)(kWoofItemClassName));
  132. //    su->SetValue(ev, length, (ODValue)(kWoofItemClassName));
  133.     
  134.     // Default Name Length
  135.     length = fDefaultName[0] + 1;
  136.     StorageUnitSetValue(su, ev, sizeof(long), (ODPtr)&length);
  137. //    su->SetValue(ev, sizeof(long), (ODValue)&length);
  138.  
  139.     // Default Name
  140.     StorageUnitSetValue(su, ev, length, (ODPtr)fDefaultName);
  141. //    su->SetValue(ev, length, fDefaultName);
  142.  
  143.     // URL Length
  144.     length = strlen(fURL) + 1;
  145.     StorageUnitSetValue(su, ev, sizeof(long), (ODPtr)&length);
  146. //    su->SetValue(ev, sizeof(long), (ODValue)&length);
  147.  
  148.     // URL
  149.     StorageUnitSetValue(su, ev, length, (ODPtr)fURL);
  150. //    su->SetValue(ev, length, fURL);
  151.     }
  152.     
  153.  
  154.  
  155. void TRealWoofItem::StreamFromStorageUnit(Environment *ev, ODStorageUnit* su)
  156.     {
  157.     long    length;
  158.  
  159.     if (fDefaultName)
  160.         {
  161.         MMFree((Ptr)fDefaultName);
  162.         ASSERT(!MemError());
  163.         fDefaultName = nil;
  164.         }
  165.     
  166.     if (fURL)
  167.         {
  168.         MMFree((Ptr)fURL);
  169.         ASSERT(!MemError());
  170.         fURL = nil;
  171.         }
  172.  
  173.     // Default Name Length
  174.     StorageUnitGetValue(su, ev, sizeof(long), (ODPtr) &length);
  175. //    (void) su->GetValue(ev, sizeof(long), &length);
  176.     fDefaultName = (StringPtr)MMAllocate(length);    // •Err
  177.     ASSERT(fDefaultName != nil);
  178.  
  179.     // Default Name
  180.     StorageUnitGetValue(su, ev, length, (ODPtr) fDefaultName);
  181. //    (void) su->GetValue(ev, length, fDefaultName);
  182.     
  183.     // URL Length
  184.     StorageUnitGetValue(su, ev, sizeof(long), (ODPtr) &length);
  185. //    (void) su->GetValue(ev, sizeof(long), &length);
  186.     fURL = (char *)MMAllocate(length);                 //•Err
  187.     ASSERT(fURL != nil);
  188.     
  189.     // URL
  190.     StorageUnitGetValue(su, ev, length, (ODPtr) fURL);
  191. //    (void) su->GetValue(ev, length, fURL);
  192.     }
  193.     
  194.  
  195.  
  196. TRealCyberItem* TRealWoofItem::Clone(Environment *ev)
  197.     {
  198.     TRealWoofItem* clone = new TRealWoofItem; //•Err
  199.     
  200.     clone->SetUpFromURL(ev, fURL, fDefaultName);
  201.     
  202.     return clone;
  203.     }
  204.  
  205.  
  206.  
  207. CICompareType TRealWoofItem::Compare(Environment *ev, TRealCyberItem* compare)
  208.     {
  209.     short            strcmpResult;
  210.     CICompareType    result;
  211.     
  212.     strcmpResult = strcmp(fURL, ((TRealWoofItem*)compare)->fURL);
  213.     
  214.     if (strcmpResult < 0)
  215.         {
  216.         result = kCICompareLessThan;
  217.         }
  218.     else if (strcmpResult > 0)
  219.         {
  220.         result = kCICompareGreaterThan;
  221.         }
  222.     else
  223.         {
  224.         result = kCICompareEqual;
  225.         }
  226.         
  227.     return result;
  228.     }
  229.  
  230.  
  231.  
  232. char* TRealWoofItem::GetURL(Environment *ev)
  233.     {
  234.     return fURL;
  235.     }
  236.  
  237.  
  238.  
  239. Handle TRealWoofItem::GetIconSuite(Environment *ev)
  240.     {
  241.     OSErr err;
  242.     Handle suite;
  243.     CResourceFocus    rf(WoofLibraryGetResFileData());
  244.     
  245.     err = ::GetIconSuite(&suite, kFirstWoofServiceShort, svAllAvailableData);
  246.     if (err != noErr)
  247.         {
  248.         suite = nil;
  249.         }
  250.     return suite;
  251.     }
  252.  
  253.  
  254.  
  255.  
  256. CyberStream* TRealWoofItem::CreateCyberStream(Environment *ev)
  257.     {
  258.     AppleCyberdog_WoofStream*        stream;
  259.     TRealWoofStream*    realWoofStream;
  260.     
  261.     stream = new AppleCyberdog_WoofStream;        // •Err
  262.     ASSERT(stream != nil);
  263.     
  264.     realWoofStream = stream->GetWoofStream(ev);
  265.     ASSERT(realWoofStream != nil);
  266.  
  267.     realWoofStream->Initialize(fURL);
  268.     
  269.     return stream;
  270.     }
  271.  
  272.  
  273.  
  274. ODTypeToken TRealWoofItem::GetContentKind(Environment *ev, ODSession* session)
  275.     {
  276.     return session->Tokenize(ev, kTextViewerKind);            //•Err, outofmem exception
  277.     }
  278.